package com.team11.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team11.server.mapper.AInformationMapper;
import com.team11.server.mapper.ClockMapper;
import com.team11.server.mapper.SInformationMapper;
import com.team11.server.pojo.AInformation;
import com.team11.server.pojo.SInformation;
import com.team11.server.service.IClockService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class ClockServiceImpl extends ServiceImpl<ClockMapper, AInformation> implements IClockService {
    @Resource
    private SInformationMapper sInformationMapper;
    @Resource
    private AInformationMapper aInformationMapper;

    @Override
    public String getStudentClock(String sNumber, String SaRealIn, String SaRealOut) {
        Integer sid = sInformationMapper.selectOne(new QueryWrapper<SInformation>().eq("s_id", sNumber)).getSid();
        List<AInformation> aInformations = aInformationMapper.selectList(new QueryWrapper<AInformation>().eq("sid", sid));
        System.out.println(aInformations);
//        AInformation aInformation=clockMapper.getClock(sNumber);
        LocalTime aRealOut = null;
        LocalTime aRealIn = null;
        if (!"".equals(SaRealIn)) {
              aRealIn = LocalTime.parse(SaRealIn, DateTimeFormatter.ofPattern("HH:mm:ss"));
        }
        if (!"".equals(SaRealOut)) {
             aRealOut = LocalTime.parse(SaRealOut, DateTimeFormatter.ofPattern("HH:mm:ss"));
        }

        LocalTime finalARealIn = aRealIn;
        LocalTime finalARealOut = aRealOut;
        aInformations.forEach(str -> {
            if (str.getAState() == 9) {
                System.out.println(finalARealIn);
                if (finalARealIn != null  && finalARealOut == null) {
//                    clockMapper.updateStudentClock(aRealIn,str.getARealOut());
                    AInformation aInformation = new AInformation();
                    aInformation.setAId(str.getAId());
                    aInformation.setARealIn(finalARealIn);
                    aInformationMapper.updateById(aInformation);
                } else if (finalARealIn == null && finalARealOut != null) {
//                    clockMapper.updateStudentClock(aRealIn,str.getARealOut());
                    AInformation aInformation = new AInformation();
                    aInformation.setAId(str.getAId());
                    aInformation.setARealOut(finalARealOut);
                    aInformationMapper.updateById(aInformation);
                }
                //clockMapper.updateStudentClock(aRealIn, aRealOut);
                AInformation aInformation = new AInformation();
                aInformation.setAId(str.getAId());
                aInformation.setARealIn(finalARealIn);
                aInformation.setARealOut(finalARealOut);
                aInformationMapper.updateById(aInformation);
            }
        });
        return "打卡成功";
    }
}
